<?php

class List_price extends MY_Controller {

	function __construct()
	{
		parent::__construct();
		$this->page=$this->module_codes['Settings'];
		$this->load->model('list_price_model' , 'LPM');
		$this->data['list_price_cat'] = $this->get_list_price_category(true);
	}

		
	function get_list_price_category($as_choices = false)
	{
		//get all cat.
		
            $this->check_page_permission('access');
			$result = $this->LPM->get_list_price_category();
			if($as_choices){
				$arr = array();
				foreach($result['info'] as $row){
					$arr[$row['id'] ] = $row['category_name'];
				}
				//print_r($arr);
				return $arr;
			}
			//return $result['info'];
	}
		
	function index()
	{		
		
            $this->check_page_permission('access');
		$result = $this->LPM->get_list_price();

		//if $result['count'] = 0 then there is no data
		//else generate array for all patient(s)
		if($result['count'] == 0){
			$data['count'] = 0;
		}
		else{
			$data['count'] = $result['count'];
			$data['result'] = $result['info'];
		}
		$this->load->view('head');
		$this->load->view('list_price/main', array_merge($this->data, $data));
		$this->load->view('footer');
	}
	
	function new_list_price($id=NULL)
	{
		
        $this->check_page_permission('add');
		$list_price_info	 	= $id ? $this->LPM->get_list_price($id) : array();
		$submit_data 			= $this->input->post('listprice_form');
		$submit_data 			= !is_array($submit_data) ? array() : $submit_data;
		
		$this->load->view('head');
		$this->load->view('list_price/list_price_form' , array_merge($this->data, array_merge($list_price_info,$submit_data)));
		$this->load->view('footer');	
	}
	
	function new_list_price2($id=NULL)
	{
		$data = $this->input->post('listprice_form');
		
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		$id =  $data['id'];
		if ($this->form_validation->run('listprice') == FALSE)
		{			
			return $this->new_list_price();
		}
		else
		{
			//Edit:
			if(isset($id) && $id!=''){
				
            $this->check_page_permission('edit');
				if($this->LPM->update_list_price_by_id($data, $id))
				redirect('list_price/' , 'refresh');
			}
			else{
				
                 $this->check_page_permission('add');
				if($this->LPM->insert_new_list_price($data))
					redirect('list_price/' , 'refresh');
			}
		}
	}
	
	function delete_list_price($id)
	{
		
         $this->check_page_permission('delete');//not used
		if(isset($id) && $id!=''){
			if($this->LPM->delete_list_price_by_id($id))
			redirect('list_price/' , 'refresh');	
		}
	}	
}

/* End of file list_price.php */
/* Location: ./system/application/controllers/list_price.php */